/**
 * 初始化系统参数详情对话框
 */
var EquipmentInfoInfoDlg = {
    equipmentInfoInfoData : {},
    validateFields: {
        equipmentName: {
            validators: {
                notEmpty: {
                    message: '设备名称不能为空'
                }
            }
        },
        communicationMode: {
            validators: {
                notEmpty: {
                    message: '通信方式不能为空'
                }
            }
        },
        ipAddress: {
            validators: {
                notEmpty: {
                    message: 'IP地址不能为空'
                }
            }
        },
        areaId: {
            validators: {
                notEmpty: {
                    message: '区域不能为空'
                }
            }
        }
        // ,
        // serialNumber: {
        //     validators: {
        //         notEmpty: {
        //             message: 'serialNumber不能为空'
        //         }
        //     }
        // }
    }
};

/**
 * 清除数据
 */
EquipmentInfoInfoDlg.clearData = function() {
    this.equipmentInfoInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EquipmentInfoInfoDlg.set = function(key, val) {
    this.equipmentInfoInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EquipmentInfoInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
EquipmentInfoInfoDlg.close = function() {
    parent.layer.close(window.parent.EquipmentInfo.layerIndex);
}
/**
 * 搜索页面关闭此对话框
 */
EquipmentInfoInfoDlg.closeOnSearch = function() {
    parent.layer.close(window.parent.Device.layerIndex);
}
/**
 * 收集数据
 */
EquipmentInfoInfoDlg.collectData = function() {
    this
        .set('id')
        .set('version')
        .set('equipmentName')
        .set('communicationMode')
        .set('ipAddress')
        .set('communicationPost')
        .set('communicationPassword')
        .set('iconType')
        .set('controllerType')
        .set('areaId')
        .set('serialNumber')
    ;
}
/**
 * 验证数据是否为空
 */
EquipmentInfoInfoDlg.validate = function () {
    $('#equipmentInfoForm').data("bootstrapValidator").resetForm();
    $('#equipmentInfoForm').bootstrapValidator('validate');
    return $("#equipmentInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 提交添加
 */
EquipmentInfoInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/add", function(data){
        Feng.success("添加成功!");
        window.parent.EquipmentInfo.table.refresh();
        EquipmentInfoInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.equipmentInfoInfoData);
    ajax.start();
}

/**
 * 搜索页面提交添加
 */
EquipmentInfoInfoDlg.addSubmitOnSearch = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/add", function(data){
        Feng.success("添加成功!");
        // window.parent.Device.table.refresh();
        EquipmentInfoInfoDlg.closeOnSearch();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.equipmentInfoInfoData);
    ajax.start();
}
/**
 * 提交修改
 */
EquipmentInfoInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/update", function(data){
        Feng.success("修改成功!");
        window.parent.EquipmentInfo.table.refresh();
        EquipmentInfoInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.equipmentInfoInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("equipmentInfoForm", EquipmentInfoInfoDlg.validateFields);

    //初始化性别选项
    $("#areaId").val($("#areaIdValue").val());
    // $("#controllerName").val($("#controllerType").val());
    $("#communicationMode").val($("#communicationModeValue").val());
    if($("#controllerType") != undefined) {
        $("#controllerType").val($("#controllerTypeValue").val());
    }
});
